/*
 * Copyright By Peak3
 * Copyright By c336bf6ff03a756989bb456b5b5e3271228d43ed38461eb5cb5755d17a2fc9b8
 * All rights reserved.
 *
 * If you are not the intended user, you are hereby notified that any use, disclosure, copying, printing, forwarding or
 * dissemination of this property is strictly prohibited. If you have got this file in error, delete it from your system.
 */

package com.vv.tools.common.message;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import lombok.Getter;
import lombok.Setter;

/**
 * @author liwen
 */
@Getter
@Setter
@JsonIgnoreProperties(ignoreUnknown = true)
public class Result<T> {

    private String code;

    private String message;

    private T data;

    /**
     * 成功返回
     */
    public static <T> Result<T> success(T t) {
        return new Result<T>()
            .ofResultEnum(ResultEnum.SUCCESS)
            .data(t);
    }

    /**
     * 失败返回
     */
    public static <T> Result<T> fail(ResultEnum resultEnum, Object... args) {
        return new Result<T>()
            .ofResultEnum(resultEnum, args);
    }

    public static <T> Result<T> error(Object... args) {
        return new Result<T>().ofResultEnum(ResultEnum.VALID_EXCEPTION, args);
    }

    private Result<T> ofResultEnum(ResultEnum resultEnum, Object... args) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.format(args);
        return this;
    }

    private Result<T> data(T t) {
        this.data = t;
        return this;
    }

}
